<template>
	<view class="v_body">
		<uni-title type="h1" title="修改密码" align="center"></uni-title>
		<view class="">
			<uni-forms :modelValue="formData" ref="form" :rules="rules">
				<uni-forms-item required name="password" label="旧密码">
					<uni-easyinput type="password" v-model="formData.password" placeholder="请输入密码" />
				</uni-forms-item>
				<uni-forms-item required name="newPass" label="新密码">
					<uni-easyinput type="password" v-model="formData.newPass" placeholder="请输入密码" />
				</uni-forms-item>
				<uni-forms-item required name="newPass1" label="确认密码">
					<uni-easyinput type="password" v-model="formData.newPass1" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>
			<button @click="editPass">确认修改</button>
		</view>
		
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msg.msgType" :message="msg.messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import { editPass } from '../../api/person.js'
	export default {
		data() {
			return {
				formData: {
					id: this.$utils.getStorage('uid'),
					username: '',
					newPass: '',
					newPass1: ''
				},
				// 表单校验规则
				rules: {
					password: {
							rules: [{
									required: true,
									errorMessage: '请输入密码',
								},
								{
									minLength: 6,
									errorMessage: '密码长度不少于 {minLength} 位',
								}
							]
					},
					newPass: {
						rules: [{
								required: true,
								errorMessage: '请输入确认密码',
							},
							{
								minLength: 6,
								errorMessage: '密码长度不少于 {minLength} 位',
							}
						]
					},
					newPass1: {
						rules: [{
								required: true,
								errorMessage: '请再次输入确认密码',
							},
							{
								minLength: 6,
								errorMessage: '密码长度不少于 {minLength} 位',
							}
						]
					}
				},
				msg: {}
			}
		},
		methods: {
			editPass(){
				this.$refs.form.validate().then(res => {
					if(this.formData.newPass !== this.formData.newPass1){
						this.msg = this.$utils.messageError('两次新密码输入不一致，请重新输入')
						this.$refs.message.open()
						this.$refs.form.resetFields();
						return 0
					}
					editPass(this.formData).then(res => {
						if(res.code === 200){
							this.msg = this.$utils.messageSuccess(res.msg)
							this.$refs.message.open()
							uni.switchTab({
								url:'person'
							})
						}else{
							this.msg = this.$utils.messageError(res.msg)
							this.$refs.message.open()
							this.$refs.form.resetFields();
						}
					})
				})
			}
		}
	}
</script>

<style>

</style>
